<?php
include_once('session.php');
require_once('php/class/familias.class.php');
$oFamilia = new familia();
$las_familias = @$oFamilia->buscar();

if ($las_familias == 0) {
  $tabla = "No hay nada que ver...";
} else {
  $tabla = "<table cellpadding='0' cellspacing='0' border='0' id='table' class='reporte'>";
  $tabla .= "<thead>
	<tr>
        <th>Id</th>
	<th>Imagen</th>
	<th>Nombre</th>
	<th>Opciones</th>
	</tr></thead><tbody>";
  $co = 0;
  
  for ($i = 0; $i < count($las_familias['ID']); $i++) {

    if ($co) {
      $color = "par";
      $co = 0;
    } else {
      $color = "impar";
      $co = 1;
    }
      
    //la foto: 
    $imagen = "images/familias/" . $las_familias['ID'][$i] . ".jpg";
    if (file_exists($imagen)) {
      $mostrar_imagen = "<img width='50px' height='50px' src='" . $imagen . "'>";
    } else {
      $mostrar_imagen = "<img width='50px' height='50px' src='images/familias/default.jpg'>";
    }

    

    $tabla .= "<tr class='" . $color . "'><td align='center'>" . $las_familias['ID'][$i] . "</td>";
    $tabla .= "<td align='center'>" . $mostrar_imagen . "</td>";
    $tabla .= "<td>" . $las_familias['NOMBRE'][$i] . "</td>";
    $tabla .= "<td align='center'><a href='familia.php?i=" . $las_familias['ID'][$i] . "'><img src='images/menu/editar.png' border='0' title='Editar'></a>";
    $tabla .= "&nbsp; | &nbsp;<a href='imagen_familia.php?i=" . $las_familias['ID'][$i] . "' rel='boxed'><img src='images/menu/imagen.png' border='0' title='cargar imagen'></a>";
    $tabla .= "&nbsp; | &nbsp;<a href='#' onclick='javascript:borrar(" . $las_familias['ID'][$i] . ");'><img src='images/menu/delete.png' border='0' title='Eliminar'></a></td></tr>";
  }
  $tabla .= "</tbody></table>";
}
?>
<html>
  <?php include('include.header_admin.html'); ?>
  <body class="bodyadmin">
    <div id="list_marcas" align="center">

      <form id="frm_bp" name="frm_bp" method="post">
        <input type="hidden" id="ACCION" name="ACCION" value="ADD">
        <input type="hidden" id="id_familia" name="id_familia">
        <table class="marco">
          <tr>
            <td height="10%">
              <?php include('menu.php'); ?>
            </td>
          </tr>
          <tr height="60px">
            <td align="center" valign="button">
              Listado de grupos de familia de art&iacute;culos
            </td>
          </tr>
          <tr>
            <td align="center" valign="top">
              <div id="DATO_HIDE" style="display:none" style="width:90%">
                <div id="DATO"></div>
              </div>
              <?php echo $tabla; ?>
            </td>
          </tr>
          <tr>
            <td height="10%" valign="bottom" align="center">
              <?php include('footer.php'); ?>
            </td>
          </tr>
        </table>
      </form>

    </div>
  </body>
  <?php include('include.footer_admin.html'); ?>
  <script>
    /****************** POP UP *****************/
    window.addEvent('domready', function() {
 
      SqueezeBox.initialize({
        size: {x: 400, y: 400}
      });
	
      SqueezeBox.assign($$('a[rel=boxed]'), {
        
        
		
        ajaxOptions: {
          method: 'GET',

        }
      });
 
    });   
    ///////////////////////////////
    var myCheck;
    myCheck = new FormCheck('frm_bp', {
      submit:false,
      display:{
        errorsLocation : 1,
        indicateErrors : 2,
        keepFocusOnError : 0,
        closeTipsButton : 1,
        checkValueIfEmpty:1,                
        showErrors : 1
      }
    });		 
 
    $('frm_bp').addEvent('submit',function(ev){
      var IsValid;
      IsValid=myCheck.isFormValid();                                                                          
      if (IsValid){ 
            
        Ejecutor();
      }    
    });
    
    
    
    function borrar(id_familia){
      if(confirm("El grupo de familia será eliminada,\n ¿Está de acuerdo?")){
        $('ACCION').value = "DELETE";
        $('id_familia').value = id_familia;
        Ejecutor();
      }
    }
    
    /////////////////////////////
    
    function Ejecutor(){        
      window.addEvent('domready', function() {      	       
        var req = new Request.JSON({
          method: 'POST',
          url: 'ajax/ajax_familia.php', 
          data: $('frm_bp'),                
          noCache:true,                     
          onRequest: function(){
            $('DATO_HIDE').show();
            $('DATO').innerHTML = 'Procesando ...'; 
          },
          onComplete: function(){
            setTimeout(function(){
              $('DATO_HIDE').fade('out').get('tween').chain(function() {
                $('DATO_HIDE').hide();
                $('DATO_HIDE').fade('in');                                                         
              });
            },2000);                                                                                                                                                                                                              
          },             
          onSuccess: function(response){
            switch(parseInt(response.RESULT)){
              case 0:
                $('DATO').innerHTML = "Operaci&oacute;n exitosa";  
                window.location='list_familias.php';
                break;
              case 1:
                $('DATO').innerHTML = response.ERROR;
                break;
            }
          }
        }).get();        
      });
    }
  </script>
</html>
